var current = 1
var pagesize = 5
var category = getCookie('category')
var search = ''
getData()
var reg = /canshu=([^&]+)/
var arr = decodeURI(location.search).match(reg)
console.log(arr);
if(arr && arr[1]){
     search = arr[1]
    getData()
}
function getData(){
    $.get('http://localhost:8888/goods/list', {
    current,
    pagesize,
    category,
    search
}).then(res => {
    console.log(res)
    if (res.code === 1) {
        console.log(res)
        var html = ''
        res.list.forEach(item => {
            html += `
            <li goods_id="${item.goods_id}">
             <a href="detail.html">
             <img src ="${item.img_big_logo}" width="150" height="180">
             <p class="goodsprice">￥${item.price}</p>
             <p class="goodsintroduce">${item.title}</p>
             </a>
            </li>
            `
        })
        $('.listt ul').html(html)
        $('.listt ul li').click(function(){
            
            setCookie('mashuaishuai',$(this).attr('goods_id'),3600)
            location.href = 'detail.html'
        })
        var totalPage = Math.ceil(res.total / pagesize)
        var pageHTML = `
                <li onclick="firstPage()">首页</li>
                <li onclick="prevPage()">上一页</li>
            `
        if(totalPage > 5){
            // 当前页<=3
            if(current <= 3){
                // 1~5
                for(var i=1;i<=5;i++){
                    pageHTML += `<li onclick="toPage(${i})" ${i === current ? 'class="active"' : ''}>${i}</li>`
                }
            }
            // 当前页>=总页数-2
            else if(current >= totalPage - 2){
                for(var i=totalPage - 4;i<=totalPage;i++){
                    pageHTML += `<li onclick="toPage(${i})" ${i === current ? 'class="active"' : ''}>${i}</li>`
                }
            }
            // 当前页在中间
            else{
                for(var i=current - 2;i<=current + 2;i++){
                    pageHTML += `<li onclick="toPage(${i})" ${i === current ? 'class="active"' : ''}>${i}</li>`
                }
            }
        }
        // 总页数不大于5
        else{
            // 1~总页数
            for(var i=1;i<=totalPage;i++){
                pageHTML += `<li onclick="toPage(${i})" ${i === current ? 'class="active"' : ''}>${i}</li>`
            }
        }
        
        pageHTML += `
                <li onclick="nextPage(${totalPage})">下一页</li>
                <li onclick="lastPage(${totalPage})">末页</li>
                <li class="count">共${totalPage}页</li>
                <li class="dao">到</li>
                <li class="pagenum"><input type="text" name="pagenum"></li>
                <li class="ye">页</li>
                <li class="gobtn" onclick="go(this, ${totalPage})"><button>确定</button></li>
            `
            if(!res.list.length){
                $('.lisbot ul').text('当前条件下暂无商品，请切换条件！')
                return
            }
            $('.lisbot ul').html(pageHTML)
    }
})
}

function toPage(i){
    
    current = i
    console.log(current);
    
    getData()
}


function nextPage(totalPage){
    current++
    if(current > totalPage){
        current = totalPage
    }
    getData()
}
function firstPage(){
    current = 1
    getData()
}
function lastPage(totalPage){
    current = totalPage
    getData()
}
function prevPage(){
    current--
    if(current < 1){
        current = 1
    }
    getData()
}
function go(ele, totalPage){
    var page = $(ele).prev().prev().find('input').val()
    if(page && !isNaN(page)){
        current = page-0
    }
    if(current > totalPage){
        current = totalPage
    }
    if(current < 1){
        current = 1
    }
    getData()
}

$('.hot').click(function(){
    $(this).addClass('active').siblings().removeClass('active')
    filter = $(this).attr('filter')
    getData()
})

// 选择折扣类型
$('[name="zk"]').change(function(){
    saleType = this.value-0
    getData()
})

// 点击价格和销量
$('.hht').click(function(){
    
})


$('.top ul li').eq(4).click(function(){
    setCookie('category','',3600)
})

$('.daohan i').click(function(){
    var ipt =$('.daohan input').val().trim()
    console.log(111);
    location.href = 'list.html?canshu='+ipt
})